<template>
  <div>
    <MyTable :list="list">
      <template #header>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #body="{ row }">
        <td>{{ row.id }}</td>
        <td>{{ row.goods_name }}</td>
        <td>{{ row.goods_price }}</td>
        <td>
          <input
            type="text"
            style="width: 100px"
            v-if="row.inputVisible"
            class="tag-input form-control"
            v-focus
            @blur="row.inputVisible = false"
            @keydown.enter="enterFn(row)"
            v-model="row.inputValue"
            @keydown.esc="row.inputValue = ''"
          />
          <button
            v-else
            class="btn btn-primary btn-sm add-tag"
            style="display: block"
            @click="row.inputVisible = true"
          >
            +Tags
          </button>
          <span
            v-for="(str, index) in row.tags"
            :key="index"
            class="badge badge-warning"
          >
            {{ str }}
          </span>
        </td>
        <td>
          <button class="btn btn-danger btn-sm" @click="delFn(row.id)">
            删除
          </button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from "..//components/MyTable.vue";
export default {
  components: {
    MyTable,
  },
  data() {
    return {
      list: [], // 商品列表数据
      
    };
  },
  // 发起axios请求
  created() {
    this.$axios({
      url: "/api/goods",
    })
      .then((res) => {
        console.log(res);
        this.list = res.data.data;
      })
      .catch((err) => {
        console.log(err);
      });
  },
  methods: {
    delFn(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
    enterFn(row) {
      if (row.inputValue.trim() === "") {
        return alert("请输入正确信息");
      } else {
        row.tags.push(row.inputValue);
        row.inputValue = "";
      }
    },
  },
  // 自动聚焦
  directives: {
    focus: {
      inserted(el) {
        el.focus();
      },
    },
  },
};
</script>

<style></style>
